#include "returnbook.h"
#include "ui_returnbook.h"

ReturnBook::ReturnBook(QWidget *parent) :
    QMyPageWidget(parent),
    ui(new Ui::ReturnBook)
{
    ui->setupUi(this);
//    QStringList strHeader;
//    strHeader<<"图书名称"<<"图书类型"<<"作者"<<"出版时间"<<"出版社";
//    QStandardItemModel* model = new QStandardItemModel(this);  //图书名称，图书类型，作者，出版时间，出版社
//    model->setHorizontalHeaderLabels(strHeader);
    ui->tableView->setAlternatingRowColors(true); // 表格数据行隔行变色
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); // 单个数据格
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//自适应对齐
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);  //选中整行
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);   //不可编辑
    GetData();
    connect(ui->tableView,&QTableView::doubleClicked,this,&ReturnBook::showBookInfo);

    ui->button_updata->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:28px '宋体';}"
  "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
   "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");


    ui->button_return->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:28px '宋体';}"
  "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
   "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");
}

ReturnBook::~ReturnBook()
{
    delete ui;
}

void ReturnBook::notify()
{
    QSqlTableModel* model = new QSqlTableModel(nullptr, DBOperation::getDatabase());
    model->setTable("lend_list");
    model->setFilter(QString("readerNumber='%0'").arg(QRoleReader::getInstance()->getReader()));
    model->select();
    ui->tableView->setModel(model);

    QString headStr[] = {"书本名称", "读者编号", "借阅开始时间", "借阅到期时间", "借阅状态"};
    for(int i=0; i < 5; i++)
    {
        model->setHeaderData(i, Qt::Horizontal, headStr[i]); // 设置表格 column 列名称
    }
    ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView::section {"
                                                     "color: brown;padding-left: 1px;border: 1px solid #6c6c6c;}");
}

void ReturnBook::GetData()
{
    List_Book.clear();
    //获取用户借书信息,在这里添加数据库select语句
    QSqlTableModel* model1 = new QSqlTableModel();
    model1->setTable("lend_list");
    model1->setFilter(QString("readerNumber='%0'").arg(QRoleReader::getInstance()->getReader()));
    model1->select();
    ui->tableView->setModel(model1);

    //遍历用户数据输出日志
    for(int i = 0; i < model1->rowCount(); i++)
    {
        QSqlRecord record = model1->record(i);
        if (QRoleReader::getInstance()->getReader().compare(record.value("readerNumber").toString()) == 0)
        {
            qDebug() << record.value("readerNumber").toString();
            qDebug() << record.value("bookName").toString();
        }
    }

    return;
}

void ReturnBook::showBookInfo()
{
    int row = ui->tableView->currentIndex().row();
    bookInfo = new BookInfo(List_Book.at(row),row);
    connect(bookInfo,SIGNAL(signals_BookInfo(int)),this,SLOT(update_Book(int)));
    bookInfo->show();
}

//void ReturnBook::update_Book(const int &index)
//{
//    List_Book.removeAt(index);
//    QStringList strHeader;
//    strHeader<<"图书名称"<<"图书类型"<<"作者"<<"出版时间"<<"出版社";
//    QStandardItemModel* model = new QStandardItemModel(this);  //图书名称，图书类型，作者，出版时间，出版社
//    model->setHorizontalHeaderLabels(strHeader);
//    ui->tableView->setAlternatingRowColors(true); // 表格数据行隔行变色
//    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); // 单个数据格
//    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);  //选中整行
//    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);   //不可编辑
//    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);   //设置单选
//    for(int i= 0 ; i < List_Book.length() ; i++){
//        model->setItem(i,0,new QStandardItem(List_Book.at(i).name));
//        model->setItem(i,1,new QStandardItem(List_Book.at(i).type_id==0?"文学类":"历史类"));
//        model->setItem(i,2,new QStandardItem(List_Book.at(i).author));
//        model->setItem(i,3,new QStandardItem(List_Book.at(i).publish_data));
//        model->setItem(i,4,new QStandardItem(List_Book.at(i).publish));
//        ui->tableView->setModel(model);
//    }
//    return;
//}

void ReturnBook::on_button_updata_clicked()
{
    GetData();
    notify();
}

void ReturnBook::on_button_return_clicked()
{
    //获取界面表模型
    QSqlTableModel* tablemodel = (QSqlTableModel*)ui->tableView->model();
    //获取模型
    QSqlTableModel* model = new QSqlTableModel(this);
    model->setTable("book_info");
    model->setFilter("");
    model->select();
    QString sql;

    //获取选择的所有行
    QList<QModelIndex> rows= ui->tableView->selectionModel()->selectedRows();
    //多行操作
    for(QModelIndex row : rows)
    {
        int nRow = row.row();
        //获取读者名称和书名称
        QString bookname = tablemodel->data(model->index(nRow, 0)).toString();
        QString readername = tablemodel->data(model->index(nRow, 1)).toString();

        if (QMessageBox::information(NULL,
                    "提示",
                    QString("是否归还：《%0》").arg(bookname),
                    QMessageBox::Yes | QMessageBox::No,
                    QMessageBox::Yes
        ) == QMessageBox::No)return;

        //删除读者借书记录
        sql = QString("DELETE FROM lend_list WHERE bookname='%0' AND readerNumber ='%1'").arg(bookname).arg(readername);
        DBOperation::getInstance()->operate(sql);

        //遍历书籍目录
        for(int j = 0; j < model->rowCount(); j++)
        {
            //获取目标书籍
            if (model->record(j).value("bookname").toString().compare(bookname) == 0)
            {
                //获取书籍数量
                int nBookNUmbers = model->record(j).value("num").toInt();
                //书本数量加一
                nBookNUmbers++;
                sql = QString("UPDATE book_info SET num=%0 WHERE bookName ='%1'").arg(nBookNUmbers).arg(bookname);
                DBOperation::getInstance()->operate(sql);
                //提示框
                QMessageBox(QMessageBox::Information, "提示", "还书操作完成!").exec();
            }
        }

        //输出日志
        qDebug()
                << "user:" << QRoleReader::getInstance()->getUserName()
                << "bookname:" << bookname
                << "readername:" << readername;
    }

    //刷新列表
    notify();
    //设置默认选择第一行
    ui->tableView->selectRow(0);
}

